package com;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test12 {
    public static void main(String[] args) throws Exception{
//write();
        read();
    }

    public static void write() throws Exception {

        String path = "HomeWork15\\dir\\序列化2.txt";
        Teacher s1 = new Teacher("张三", 13, "男");
        Teacher s2 = new Teacher("李四", 23, "男");
        Teacher s3 = new Teacher("王五", 33, "女");
        ArrayList<Teacher> tea = new ArrayList<>();
        tea.add(s1);
        tea.add(s2);
        tea.add(s3);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
        oos.writeObject(tea);
        oos.close();

    }

    public static void read() throws Exception {
        String path = "HomeWork15\\dir\\序列化2.txt";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
        Object o = ois.readObject();
        ois.close();
        ArrayList<Teacher> tea = (ArrayList<Teacher>) o;
        tea.forEach(t -> System.out.println(t));
    }
}
